package ro.msg.quiz.validator;

import ro.msg.quiz.domain.MultipleChoiceQuestion;
import ro.msg.quiz.domain.Question;
import ro.msg.quiz.exception.MyException;

//AUTHORS: Elod, Marco

//Class that validates a MultipleChoiceQuestion
public class MultipleChoiceQuestionValidator extends QuestionValidator {

    @Override
    public void validate(Question o) throws MyException {
	StringBuilder sb = new StringBuilder();

	if (o instanceof MultipleChoiceQuestion) {
	    MultipleChoiceQuestion q = (MultipleChoiceQuestion) o;
	    if (q.getOptions().size() < 1) {
		sb.append("The question doesn't have any possible answers!\n");
	    }
	    if (q.getCorrectIndexes().size() < 0 || q.getCorrectIndexes().size() > q.getOptions().size()) {
		sb.append("The number of correct answers must be between 0 and the number of possible answers!\n");
	    }
	    forStatement: for (int i : q.getCorrectIndexes()) {
		if (i < 0 || i > q.getOptions().size()) {
		    sb.append("The index of the correct answer must be between 0 and the number of possible answers!\n");
		    break forStatement;

		}
	    }
	} else
	    sb.append("The object is not an instance of SingleChoiceQuestion!\n");

	if (sb.length() > 0) {
	    throw new MyException(sb.toString());
	}
    }
}
